/**
 * 
 */
package custom.swing;

import javax.swing.AbstractListModel;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Comparator;

/**
 * @author David Armstrong
 *
 */
public class SortedMapListModel<K, V> extends AbstractListModel {

    public static final long serialVersionUID = 1;
    private TreeMap<K, V> delegate = null;

    public SortedMapListModel() {
        delegate = new TreeMap<K, V>();
    }

    public SortedMapListModel(final Comparator<? super K> c) {
        delegate = new TreeMap<K, V>(c);
    }

    public int getSize() {
        return delegate.size();
    }

    public V getElementAt(int index) {
        ArrayList<V> values = new ArrayList<V>(delegate.values());
        return values.get(index);
    }

    public Object[] toArray() {
        return delegate.values().toArray();
    }

    public V[] toArray(V[] values) {
        values = delegate.values().toArray(values);
        return values;
    }

    public int size() {
        return delegate.size();
    }

    public boolean isEmpty() {
        return delegate.isEmpty();
    }

    public boolean containsValue(final Object value) {
        return delegate.containsValue(value);
    }

    public boolean containsKey(final Object key) {
        return delegate.containsKey(key);
    }

    public int indexOf(final Object elem) {
        ArrayList<V> values = new ArrayList<V>(delegate.values());
        return values.indexOf(elem);
    }

    public int indexOf(final Object elem, final int index) {
        Vector<V> values = new Vector<V>(delegate.values());
        return values.indexOf(elem, index);
    }

    public int lastIndexOf(final Object elem) {
        ArrayList<V> values = new ArrayList<V>(delegate.values());
        return values.lastIndexOf(elem);
    }

    public int lastIndexOf(final Object elem, final int index) {
        Vector<V> values = new Vector<V>(delegate.values());
        return values.lastIndexOf(elem, index);
    }

    public V get(final int index) {
        ArrayList<V> values = new ArrayList<V>(delegate.values());
        return values.get(index);
    }

    public V firstElement() {
        Vector<V> values = new Vector<V>(delegate.values());
        return values.firstElement();
    }

    public V lastElement() {
        Vector<V> values = new Vector<V>(delegate.values());
        return values.lastElement();
    }

    public V put(final K key, final V value) {
        V v = delegate.put(key, value);
        ArrayList<V> values = new ArrayList<V>(delegate.values());
        int index = values.indexOf(value);
        fireIntervalAdded(this, index, index);
        return v;
    }

    /*
    public void putAll(Map<? extends K,? extends V> map)
    {
    delegate.putAll(map);
    }
     */
    public V remove(final int index) {
        ArrayList<K> keys = new ArrayList<K>(delegate.keySet());
        K k = keys.get(index);
        V v = delegate.remove(k);
        fireIntervalRemoved(this, index, index);
        return v;
    }

    public void clear() {
        int lastIndex = delegate.size() - 1;
        delegate.clear();
        if (lastIndex >= 0) {
            fireIntervalRemoved(this, 0, lastIndex);
        }
    }

    public String toString() {
        return delegate.toString();
    }
}
